# download_image.py
import urllib.request
import os

def download_image(soup, url, save_folder="download"):
    """下载电影图片"""
    src = soup.find("div", attrs={"class": "pic"}).find("img")["src"]
    full_src = urllib.request.urlopen(urllib.request.urljoin(url, src))
    data = full_src.read()
    
    # 获取图片名
    p = src.rfind("/")
    name = src[p+1:]
    
    # 创建保存目录
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)
    
    save_path = os.path.join(save_folder, name)
    try:
        with open(save_path, "wb") as f:
            f.write(data)
        print(f"图片已保存到: {save_path}")
    except Exception as err:
        print(f"Error downloading image: {err}")
